<?php

require_once 'Ext/User/Identify/Interface.php';

class Ext_User_Identify_Adapter_Db extends Ext_User_Identify_Abstract
{
    const IDENTITY_NOT_FOUND = 'identityNotFound';
    const CREDENTIAL_INVALID = 'credentialInvalid';
    protected $_messageTemplates = array(
        self::IDENTITY_NOT_FOUND => "A record with the supplied identity could not be found.",
        self::CREDENTIAL_INVALID => "Supplied credential is invalid.",
    );
    
    public function verify()
    {
        $result = array(
            'code'     => Ext_User_Identify_Result::FAILURE,
            'data'     => array(),
            'messages' => array(),
        );
        
        $userId = $username = $email = '';
        $em = new Zend_Validate_EmailAddress();
        if($em->isValid($this->_data->account)){
            $email = $this->_data->account;
        }else{
            $username = $this->_data->account;
        }
        
        if(!$userId){
            if($email){
                $accountBaseEmail = App_Model_DbTable_User_Account_Base_Email::getInstance()->getRowByEmail($email);
                if($accountBaseEmail){
                    $userId = $accountBaseEmail['userId'];
                    //$email  = $this->_data->account;
                }
            }
        }
        if(!$userId){
            $settings = Zend_Registry::get('settings');
            if($settings['userAccountUsername']){
                $accountBaseUsername = App_Model_DbTable_User_Account_Base_Username::getInstance()->getRowByUsername($username);
                if($accountBaseUsername){
                    $userId   = $accountBaseUsername['userId'];
                    $username = $this->_data->account;
                }
            }
        }
        if(!$userId){
            $result['messages'][] = $this->_messager->createMessage(self::IDENTITY_NOT_FOUND);
            return new Ext_User_Identify_Result($result['code'], $result['data'], $result['messages']);
        }

        $accountBase = App_Model_DbTable_User_Account_Base::getInstance()->getRowByUserId($userId);
        if(!$accountBase){
            $result['messages'][] = $this->_messager->createMessage(self::IDENTITY_NOT_FOUND);
            return new Ext_User_Identify_Result($result['code'], $result['data'], $result['messages']);
        }
        
        if($accountBase['password'] != md5($this->_data->password)){
            $result['messages'][] = $this->_messager->createMessage(self::CREDENTIAL_INVALID);
            return new Ext_User_Identify_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $userProfileRow = App_Model_DbTable_User_Profile::getInstance()->getRowByUserId($userId);
        if(!$userProfileRow){
            $row = array (
                'userId'         => $userId,
                'username'       => $username,
                'email'          => $email,
            );
            App_Model_DbTable_User_Profile::getInstance()->addNew($row);
            
        }else{
            $row = array();
            if(!$userProfileRow['username'] && $username){
                $row['username'] = $username;
            }
            if(!$userProfileRow['email'] && $email){
                $row['email'] = $email;
            }
            
            if($row){
                App_Model_DbTable_User_Profile::getInstance()->addOld($row, $userId);
            }
        }
        
        $result['code'] = Ext_User_Identify_Result::SUCCESS;
        $result['data'] = $userId;
        return new Ext_User_Identify_Result($result['code'], $result['data'], $result['messages']);
    }
}
